package ctr_layer;

import java.util.List;

import model_layer.Product;
import db_layer.ProductDB;

public class ProductCtr
{
	private ProductDB prod_db;
	public ProductCtr()
	{
		prod_db = new ProductDB();
	}
	
	public Product find_product_by_id(String id)
	{
		return prod_db.find_product("id", Integer.parseInt(id));
	}
	
	public Product find_product_by_name(String name)
	{
		if(name == null || name.equals(""))
		{
			return null;
		}
		System.out.println(name);
		return prod_db.find_product("name", name);
	}
	
	public List<Product> find_all_products()
	{
		return prod_db.find_all_products();
	}
	
	public List<Product> find_non_deleted_products()
	{
		return prod_db.find_particular_products("deleted", 0);
	}
	
	public List<Product> find_deleted_products()
	{
		return prod_db.find_particular_products("deleted", 1);
	}
	
	public int insert_product(String id, String name, int quantity, int min_amount, float last_retail_price, float price)
	{
		Product prod = new Product();
		prod.setId(id);
		prod.setLast_retail_price(last_retail_price);
		prod.setDeleted(false);
		prod.setMin_amount(min_amount);
		prod.setName(name);
		prod.setPrice(price);
		prod.setQuantity(quantity);
		return prod_db.insert_product(prod);
	}
	
	public int update_product(Product prod)
	{
		return prod_db.update_product(prod);
	}
	
	public boolean delete_product(int id)
	{
		Product prod = prod_db.find_product("id", id);
		prod.setDeleted(true);
		prod.setQuantity(0);
		return prod_db.update_product(prod) == 1;
	}
	
	public boolean restore_product(int id)
	{
		Product prod = prod_db.find_product("id", id);
		prod.setDeleted(false);
		return prod_db.update_product(prod) == 1;
	}
	
	public boolean id_exists(String id)
	{
		return prod_db.find_product("id", Integer.parseInt(id)) != null;
	}
	
	public boolean name_exists(String name)
	{
		return prod_db.find_product("name", name) != null;
	}
	
	

}
